/**
 * Copyright (c) 2017 www.wufe.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * @Title: SuiXunCallableInterceptor.java    
 * @Package com.suixun.interceptor    
 * @Description:   
 * @author wufeluli   276593209@qq.com
 * @date 2017年12月12日 下午3:23:37    
 * @version V1.0    
*/
package com.suixun.interceptor;

import java.util.concurrent.Callable;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.async.CallableProcessingInterceptor;

/**    
* 类描述：  Callable任务的超时处理  ,用于异步servlet 调用
* 创建人：luli     
* 创建时间：2017年12月12日 下午3:23:37    
*/
public class SuiXunCallableInterceptor implements CallableProcessingInterceptor
{

	/*
	* <p>Title: beforeConcurrentHandling</p>   
	* <p>Description: </p>   
	* @param request
	* @param task
	* @throws Exception    
	* @see org.springframework.web.context.request.async.CallableProcessingInterceptor#beforeConcurrentHandling(org.springframework.web.context.request.NativeWebRequest, java.util.concurrent.Callable)   
	*/
	@Override
	public <T> void beforeConcurrentHandling(NativeWebRequest request, Callable<T> task) throws Exception
	{
	}

	/*
	* <p>Title: preProcess</p>   
	* <p>Description: </p>   
	* @param request
	* @param task
	* @throws Exception    
	* @see org.springframework.web.context.request.async.CallableProcessingInterceptor#preProcess(org.springframework.web.context.request.NativeWebRequest, java.util.concurrent.Callable)   
	*/
	@Override
	public <T> void preProcess(NativeWebRequest request, Callable<T> task) throws Exception
	{
	}

	/*
	* <p>Title: postProcess</p>   
	* <p>Description: </p>   
	* @param request
	* @param task
	* @param concurrentResult
	* @throws Exception    
	* @see org.springframework.web.context.request.async.CallableProcessingInterceptor#postProcess(org.springframework.web.context.request.NativeWebRequest, java.util.concurrent.Callable, java.lang.Object)   
	*/
	@Override
	public <T> void postProcess(NativeWebRequest request, Callable<T> task, Object concurrentResult) throws Exception
	{
	}

	/*
	* <p>Title: handleTimeout</p>   
	* <p>Description: </p>   
	* @param request
	* @param task
	* @return
	* @throws Exception    
	* @see org.springframework.web.context.request.async.CallableProcessingInterceptor#handleTimeout(org.springframework.web.context.request.NativeWebRequest, java.util.concurrent.Callable)   
	*/
	@Override
	public <T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception
	{
		 HttpServletResponse servletResponse = request.getNativeResponse(HttpServletResponse.class);
         if (!servletResponse.isCommitted()) {
            servletResponse.setContentType("text/plain;charset=utf-8");
            servletResponse.getWriter().write("超时了");
            servletResponse.getWriter().close();
         }
         return null;
	}

	/*
	* <p>Title: afterCompletion</p>   
	* <p>Description: </p>   
	* @param request
	* @param task
	* @throws Exception    
	* @see org.springframework.web.context.request.async.CallableProcessingInterceptor#afterCompletion(org.springframework.web.context.request.NativeWebRequest, java.util.concurrent.Callable)   
	*/
	@Override
	public <T> void afterCompletion(NativeWebRequest request, Callable<T> task) throws Exception
	{
	}

}
